// pages/result/result.js
const app = getApp()
import http from '../../utils/http.js'
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:'',
    nowTimes:'',
    orderInfo:{},
    status: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this
    let nowTime = new Date()
    let month = Number(nowTime.getMonth()+1)>=10? Number(nowTime.getMonth()+1):'0'+ Number(nowTime.getMonth()+1)
    let day = Number(nowTime.getDate())>=10?nowTime.getDate():'0'+(nowTime.getDate())
    let min = Number(nowTime.getMinutes())>=10?nowTime.getMinutes():'0'+(nowTime.getMinutes())
    let hour = Number(nowTime.getHours())>=10?nowTime.getHours():'0'+(nowTime.getHours())

    let nowTimes = nowTime.getFullYear()+'.'+month+'.'+day+' '+hour+':'+min
    that.setData({
      orderId:options.orderId,
      nowTimes:nowTimes,
      status: Number(options.status)
    })
    that.setData({
      orderInfo: wx.getStorageSync('orderInfo')
    })
  },

  // 核销
  orderEd(){
    let that = this
    if (this.data.status === 1) {
      return
    }
    if (this.data.status === 2) {
      Dialog.confirm({
        title: '温馨提示',
        message: '该会员的预约已过期，是否继续核销',
        confirmButtonText: '继续核销',
        cancelButtonText: '不核销'
      }).then(() => {
        that.orderRequest()
      })
    } else {
      this.orderRequest()
    }
  },

  orderRequest() {
    let that = this;
    // '/api/store/v1/appointment/update/status',// 修改用户订单状态  0.预约成功 1.使用成功 2.预约未使用 3.取消预约
    http.post('/api/store/v1/appointment/update/status', {
      id: that.data.orderId,
      status: 1 //使用成功
    }).then(res => {
      console.log(res,"修改订单状态")
      if(res.code == 200 ){
        wx.showToast({
          title: '到店成功',
          icon: 'none',
          duration: 2000
        });
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
          })
        },1500);
      } else {
        wx.showToast({
          icon: "none",
          title: res.message,
          duration: 2000
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
          })
        },1500);
      }
    })
  }
})